package com.example.mybiadushoujiweishi.activity.acivitys.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.Versioninfo;
import com.example.mybiadushoujiweishi.activity.acivitys.services.MyService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/16 0016.
 */

public class UpdateVersionFragment extends Basefragment {
    @BindView(R.id.updateversion)
    TextView updateversion;
    Unbinder unbinder;
    @BindView(R.id.ok)
    Button ok;
    @BindView(R.id.no)
    Button no;

    @Override
    public int getContentXml() {
        return R.layout.updateversionfragmentlayout;
    }

    public String getMsg() {
        no.setVisibility(View.GONE);
        Bundle bundle1 = getArguments();
        String msg = bundle1.getString("msg");

        return msg;
    }

    public static UpdateVersionFragment geUpdateVersionFragment(String msg) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        updateVersionFragment.setArguments(bundle);//设置值   传递给Activity
        return updateVersionFragment;
    }

    public static UpdateVersionFragment geupdateversionfragment(Versioninfo versioninfo) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("versioninfo", versioninfo);
        updateVersionFragment.setArguments(bundle);
          return updateVersionFragment;
    }

    @Override
    public void getContentView(View view) {

        Bundle bundle = getArguments();
        Versioninfo versioninfo = (Versioninfo) bundle.getSerializable("versioninfo");
        if (versioninfo != null) {
            no.setVisibility(View.VISIBLE);
            updateversion.setText(versioninfo.getApkinfo());

        } else {
            updateversion.setText(getMsg());
        }

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        unbinder.unbind();
    }

    @OnClick({R.id.ok, R.id.no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ok:
                Bundle bundle = getArguments();
                Versioninfo versioninfo = (Versioninfo) bundle.getSerializable("versioninfo");
                if(versioninfo!=null) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), MyService.class);
                    MyService myService=new MyService();
                    myService.createNotification();
                    intent.putExtra("apkurl", versioninfo.getApkurl());
                    getActivity().startService(intent);

                }
                dismiss();
                break;
            case R.id.no:
             dismiss();
                break;
        }
    }
}
